﻿using InfoReaderLib.Info;
using InfoReaderLib.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;

namespace InfoReaderLib
{
    public sealed class Reader
    {
        public IInfoSource Source { get; set; }

        private string _ParserSetting;
        public string ParserSetting
        {
            get
            {
                return this.ParserSetting;
            }
            set
            {
                this._ParserSetting = value;
                if (this._Parser != null)
                    this._Parser.SettingJSON = this._ParserSetting;
            }
        }

        private IParser _Parser;

        /// <summary>
        /// Initialize reader
        /// </summary>
        public void Init()
        {
            if (this._Parser != null) return;
            if (this.Source == null) return;

            // obtain parser by reflection
            Type t = Type.GetType("InfoReaderLib.Parser." + this.Source.ParserName + ".Parser");
            this._Parser = (IParser)Activator.CreateInstance(t);
            this._Parser.DataSource = this.Source;
            this._Parser.SettingJSON = this._ParserSetting;
        }

        public IAsyncOperation<IInfo> Parse()
        {
            if (this._Parser == null)
                return null;
            return this._Parser.Parse();
        }

        public IAsyncOperation<IInfo> Parse(IInfoDetail detailSource)
        {
            if (this._Parser == null)
                return null;
            return this._Parser.Parse(detailSource);
        }
    }
}
